<?php

class Ajax {

    public static function action_index() {
        
    }

    /**
     * 
     * @param type $param
     * 
     */
    public static function action_login($param) {
        $data = Frontend::findUserLogin($param["email"], $param["pass"]);
        if (iterator_to_array($data) == null) {
            echo "Email hoặc mật khẩu không đúng.";
        } else {
            foreach ($data as $_data) {
                Session::set_session("fullname", $_data["fullname"]);
                Session::set_session("email", $_data["email"]);
                Session::set_session("user_id", $_data["_id"]);
            }
            echo "1";
        }
    }

    /**
     * 
     * @param type $param
     * 
     */
    public static function action_change_pass($param) {
        $data = Frontend::findUserByPass($param["curpass"], $param["id"]);
        if (iterator_to_array($data) == null) {
            echo "Mật khẩu cũ không đúng.";
        } else {
            try {
                foreach ($data as $_data) {
                    $_data = $_data + array("newpass" => $param["pass"]);
                    Sys::updateUserPass($_data);
                    echo "Thay đổi thành công.";
                }
            } catch (Exception $exc) {
                echo "Thay đổi mật khẩu lỗi.";
                die();
            }
        }
    }

    /**
     * 
     * @param type $param
     * 
     */
    public static function action_change_user_info($param) {
        $data = Frontend::findUserById($param["id"]);
        if (iterator_to_array($data)) {
            foreach ($data as $_data) {
                $param = $param + array("email" => $_data["email"]);
                $param = $param + array("password" => $_data["password"]);
                $param = $param + array("joindate" => $_data["joindate"]);
                $param = $param + array("gold" => $_data["gold"]);
                $param = $param + array("silver" => $_data["silver"]);
                $param = $param + array("status" => $_data["status"]);
                try {
                    Sys::updateUserInfo($param);
                    echo "Thay đổi thông tin thành công.";
                } catch (Exception $exc) {
                    echo "Thay đổi thông tin lỗi";
                }
            }
        }
    }
    /**
     * 
     * @param type $param
     * 
     */
    public static function action_admin_login($param) {
        $data = Frontend::findAdminLogin($param["username"], $param["password"]);
        if (iterator_to_array($data) == null) {
            echo "0";
        } else {
            foreach ($data as $_data) {
                if ($param["rememberme"] == "on") {
                    Session::set_cookie("username", $_data["username"], time() * 3600);
                    Session::set_cookie("id", $_data["_id"], time() * 3600);
                    Session::set_session("username", $_data["username"]);
                    Session::set_session("id", $_data["_id"]);
                } else {
                    Session::set_session("username", $_data["username"]);
                    Session::set_session("id", $_data["_id"]);
                }
            }
            echo "1";
        }
    }
    /**
     * 
     * @param type $param
     * 
     */
    public static function action_add_odd($param) {
        $base_decode = base64_decode($param["values"]);
        $json_decode = json_decode($base_decode);
        $id = Frontend::findOddById($json_decode->id);
        $flag = false;
        if (iterator_to_array($id)) {
            if (isset($_SESSION["cart"])) {
                foreach ($_SESSION["cart"] as $_cart) {
                    $session_decode = base64_decode($_cart);
                    $session_decode = json_decode($session_decode);
                    if (($json_decode->id == $session_decode->odd_id) && ($json_decode->value == $session_decode->odd_value)) {
                        $flag = true;
                    }
                }
            }
            if (!$flag) {
                foreach ($id as $_id) {
                    $value = Frontend::findOddByValue($json_decode->value);
                    if (iterator_to_array($value)) {
                        $arr = array(
                            "odd_id" => "$json_decode->id",
                            "odd_value" => "$json_decode->value",
                            "match_id" => $_id['matchID'],
                        );
                        $json_encode = json_encode($arr);
                        $base_encode = base64_encode($json_encode);
                        $_SESSION["cart"][] = $base_encode;
                        echo count($_SESSION["cart"]);
                    } else {
                        echo count($_SESSION["cart"]);
                    }
                }
            } else {
                echo "0";
            }
        }
    }
    
    public static function action_update_datcuoc($param){
        $base_decode = base64_decode($param["values"]);
        $json_decode = json_decode($base_decode, true);
        if(!$json_decode["money"]){
            $total_money = $json_decode["odd_value"] * $param["money"];
            $json_decode = $json_decode + array("money" => $param["money"]);
            $json_decode = $json_decode + array("total_money" => $total_money);
            $json_decode = $json_decode + array("index_key" => $param["index_key"]);
        } else {
            $total_money = $json_decode["odd_value"] * $param["money"];
            $json_decode["money"] = $param["money"];
            $json_decode["total_money"] = $total_money;
            $json_decode["index_key"] = $param["index_key"];
        }
        $json_encode = json_encode($json_decode);
        $base_encode = base64_encode($json_encode);
        $_SESSION["cart"][$param["key"]] = $base_encode;
        echo $total_money;
    }
    
    public static function action_update_all_datcuoc($param){
        
    }
    
    public static function action_delete_datcuoc($param){
        unset($_SESSION["cart"][$param["key"]]);
        require_once 'app/views/frontend/_danhsachdudoan.php';
    }

}

?>

